/*
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
 */

package io.ionic.starter;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import com.marianhello.bgloc.LocationService;
import com.tenforwardconsulting.cordova.BackgroundGeolocationPlugin;

import org.apache.cordova.*;

import java.util.Timer;
import java.util.TimerTask;

import io.ionic.starter.keep.Contants;
import io.ionic.starter.keep.DaemonService;
import io.ionic.starter.keep.JobSchedulerManager;
import io.ionic.starter.keep.MediaToKeepService;
import io.ionic.starter.keep.ScreenManager;
import io.ionic.starter.keep.ScreenReceiverUtil;
import io.ionic.starter.keep.SystemUtils;

public class MainActivity extends CordovaActivity {

  private ScreenReceiverUtil mScreenListener;
  private ScreenManager mScreenManager;
  private JobSchedulerManager mJobManager;
  private Timer mRunTimer;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // enable Cordova apps to be started in the background
    Bundle extras = getIntent().getExtras();
    if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
      moveTaskToBack(true);
    }

    // Set by <content src="index.html" /> in config.xml
    loadUrl(launchUrl);

//    mScreenListener = new ScreenReceiverUtil(this);
//    mScreenManager = ScreenManager.getScreenManagerInstance(this);
//    mScreenListener.setScreenReceiverListener(mScreenListenerer);
    mJobManager = JobSchedulerManager.getJobSchedulerInstance(this);
    mJobManager.startJobScheduler();
    startDaemonService();
    startMediaService();
  }

  @Override
  protected void onResume() {
    super.onResume();
    if (mRunTimer == null) {
      startRunTimer();
    }
  }

  private ScreenReceiverUtil.SreenStateListener mScreenListenerer = new ScreenReceiverUtil.SreenStateListener() {
    @Override
    public void onSreenOn() {
      mScreenManager.finishActivity();
    }

    @Override
    public void onSreenOff() {
      mScreenManager.startActivity();
    }

    @Override
    public void onUserPresent() {
    }
  };


  private void startDaemonService() {
    Intent intent = new Intent(MainActivity.this, DaemonService.class);
    startService(intent);
  }

  private void stopDaemonService() {
    Intent intent = new Intent(MainActivity.this, DaemonService.class);
    stopService(intent);
  }


  private void stopMediaService() {
    Intent intent = new Intent(MainActivity.this, MediaToKeepService.class);
    stopService(intent);
  }

  private void startMediaService() {
    Intent intent = new Intent(MainActivity.this, MediaToKeepService.class);
    startService(intent);
  }

  private void startRunTimer() {
    TimerTask mTask = new TimerTask() {
      @Override
      public void run() {
            if(!SystemUtils.isServiceRunning(MainActivity.this, "com.marianhello.bgloc.LocationService")
              && BackgroundGeolocationPlugin.ACTION_CURRENT.equals("start")) {
              Intent locationServiceIntent = new Intent(MainActivity.this, LocationService.class);
              // start service to keep service running even if no clients are bound to it
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                MainActivity.this.startForegroundService(locationServiceIntent);
              } else {
                MainActivity.this.startService(locationServiceIntent);
              }
            }
           // Toast.makeText(getApplicationContext(), "" + SystemUtils.isServiceRunning(MainActivity.this, "com.marianhello.bgloc.LocationService"), Toast.LENGTH_SHORT).show();
      }
    };
    mRunTimer = new Timer();
    mRunTimer.schedule(mTask, 1000, 1000 * 60);
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    stopDaemonService();
    stopMediaService();
    mScreenListener.stopScreenReceiverListener();
  }
}
